/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.protocol.HTTP;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;


public class Communication {

	
	
	/*
	 * Check is device online
	 */
	static public boolean isOnline(ConnectivityManager cm) {
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		return (netInfo != null && netInfo.isConnected());
	}// isOnline

	
	
	/*
	 * Check is source available
	 */
	static public boolean isSourceAvalable(String source) {
		try {
			URL url = new URL(source);

			HttpURLConnection urlc = (HttpURLConnection) url.openConnection();

			urlc.setRequestProperty("Connection", "close");
			urlc.setConnectTimeout(1000 * 7); // mTimeout is in seconds
			urlc.connect();
			
			if (urlc.getResponseCode() == 200) {
				return true;
			} else {
				return false;
			}
			
		} catch (MalformedURLException e1) {
			return false;
		} catch (IOException e) {
			return false;
		} 
	}// isSourceAvalable

	
	
	/*
	 * Post SOAP
	 */
	static public String postSOAP(String targetURL, String envelope){
		
		HttpPost httppost = new HttpPost(targetURL);          
		StringBuffer response = new StringBuffer();
		
		try {
			StringEntity se = new StringEntity(envelope, HTTP.UTF_8);


			se.setContentType("text/xml");  
			httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
			httppost.setEntity(se);  
	
			HttpClient httpclient = new DefaultHttpClient();
			BasicHttpResponse httpResponse = null;
			

			httpResponse = (BasicHttpResponse) httpclient.execute(httppost);
			BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
			String line;
			
			while ((line = rd.readLine()) != null) {
				response.append(line);
				response.append('\r');
			}
			rd.close();
		
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return response.toString();
	}//postSOAP
	
	
	
	
	/*
	 * Post SOAP
	 */
	static public String requestAdHogRequest(String targetURL, String envelope){
		
		HttpPost httppost = new HttpPost(targetURL);          
		
		StringBuffer response = new StringBuffer();
		
		try {
			StringEntity se = new StringEntity("__rdxml=" + envelope, HTTP.UTF_8);

			se.setContentType("application/x-www-form-urlencoded");
			httppost.setHeader("Authorization", "GoogleLogin auth=" + AuthService.authToken);
			//httppost.setHeader("clientEmail", AuthService.accountEmail);        
			httppost.setHeader("clientCustomerId", AuthService.clientId);
			httppost.setHeader("developerToken", AuthService.devToken);
			
			httppost.setEntity(se); 
			
			HttpClient httpclient = new DefaultHttpClient();
			BasicHttpResponse httpResponse = null;

			httpResponse = (BasicHttpResponse) httpclient.execute(httppost);
			BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
			String line;
			
			while ((line = rd.readLine()) != null) {
				response.append(line);
				response.append('\r');
			}
			rd.close();
		
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return response.toString();
	}//requestAdHogRequest

}
